import 'package:erp/data/api/user_api.dart';
import 'package:erp/data/model/auth_dto.dart';
import 'package:erp/main_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

class PasswordController extends GetxController {
  var originPasswordTextEditingController = new TextEditingController();
  var newPasswordTextEditingController = new TextEditingController();
  var repeatPasswordTextEditingController = new TextEditingController();

  var showOriginPassword = false.obs;
  var showNewPassword = false.obs;
  var showRepeatPassword = false.obs;

  @override
  void onInit() {
    super.onInit();
  }

  void submit() async {
    var ms = Get.find<MainState>();
    if (originPasswordTextEditingController.value.text.isEmpty) {
      EasyLoading.showToast("原始密码不能为空");
      return;
    }
    if (newPasswordTextEditingController.value.text.isEmpty) {
      EasyLoading.showToast("新密码不能为空");
      return;
    }
    if (repeatPasswordTextEditingController.value.text.isEmpty) {
      EasyLoading.showToast("确认密码不能为空");
      return;
    }
    if (repeatPasswordTextEditingController.value.text != newPasswordTextEditingController.value.text) {
      EasyLoading.showToast("2次输入密码不一致");
      return;
    }

    var id = ms.userInfo.value?.id;
    AuthDTO dto = AuthDTO(
        id: id, newPwd: newPasswordTextEditingController.text, userPwd: originPasswordTextEditingController.text);
    await LoginApi.updatePwd(dto);
    EasyLoading.showSuccess("修改成功,5秒后自动退出登录");
    Future.delayed(Duration(seconds: 5), () {
      ms.clearAuth();
    });
  }
}
